﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using static Win32API.WinApi;

namespace Win32API
{
    public class InternetExplorer
    {
        /// <summary>
        /// 句柄
        /// </summary>
        public IntPtr HWND { get; set; }

        /// <summary>
        /// 文件夹路径
        /// </summary>
        public string LocationURL { get; set; }

        /// <summary>
        /// 关闭窗口,关闭文件夹
        /// </summary> 
        public void Quit()
        {
            SendMessage(HWND, (int)WM.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        }
    }

    /// <summary>
    /// 遍历桌面资源管理器
    /// </summary>
    public class ShellWindows
    {
        public List<InternetExplorer> Enumerator = new List<InternetExplorer>();

        /// <summary>
        /// 获取桌面所有文件夹的路径
        /// </summary>
        /// <returns></returns>
        public ShellWindows()
        {
            var wndList = new List<WindowInfo>();
            GetAllDesktopWindows(wndList);
            foreach (var item in wndList)
            {
                if (item.ClassName == "CabinetWClass")
                { 
                    var fi = FindWindowEx(item.Hwnd, 0, "WorkerW", null);
                    if (fi != IntPtr.Zero)
                    {
                        fi = FindWindowEx(fi, 0, "ReBarWindow32", null);
                        if (fi != IntPtr.Zero)
                        {
                            fi = FindWindowEx(fi, 0, "Address Band Root", null);
                            if (fi != IntPtr.Zero)
                            {
                                fi = FindWindowEx(fi, 0, "msctls_progress32", null);
                                if (fi != IntPtr.Zero)
                                {
                                    fi = FindWindowEx(fi, 0, "Breadcrumb Parent", null);
                                    if (fi != IntPtr.Zero)
                                    {
                                        fi = FindWindowEx(fi, 0, "ToolbarWindow32", null);//资源管理器

                                        //知识:toolbar上的按钮没有handler,要用发送通知信息
                                        if (fi != IntPtr.Zero)
                                        {
                                            var sb = new StringBuilder(256);
                                            //获取窗口名称-路径地址
                                            GetWindowText(fi, sb, sb.Capacity);
                                            string path = sb.ToString();
                                            path = path.Substring(4, path.Length - 4);//4表示"地址: "长度

                                            var ie = new InternetExplorer
                                            {
                                                HWND = item.Hwnd,
                                                LocationURL = path
                                            };
                                            Enumerator.Add(ie);
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }
    }




}
